﻿/*

@version 0.0.1
@author kaiser
@deprecated 移动端手指触摸事件库

*/
(function() {
	"use strict";

	var _ver = "0.0.1";

	/****************
	@deprecated Util
	***************/
	var util = {
		touch: 'ontouchstart' in document.documentElement,

		$: function(selector) {
			return document.querySelector(selector);
		},
		getEventInfo: function(e) {
			var _e = {};
			_e.pageX = util.touch ? e.changedTouches[0].pageX : e.pageX;
			_e.pageY = util.touch ? e.changedTouches[0].pageY : e.pageY;
			_e.target = e.target;
			util.touch && (_e.changedTouches = e.changedTouches);
			return _e;
		}

	};

	/****************
		touch events
	*****************/
	var tStart = util.touch ? 'touchstart' : 'mousedown';
	var tMove = util.touch ? 'touchmove' : 'mousemove';
	var tEnd = util.touch ? 'touchend' : 'mouseup';
	/***********
	@deprecated 点击事件
	**********/
	var _tap = function(callback) {
		this.addEventListener(tStart, function(e) {
			var _e = util.getEventInfo(e);
			_e.type = 'tap';
			callback.call(this, _e);
		}, false);
	};

	/*************
	@deprecated logtap
	*************/
	var _longtap = function(callback) {
		var interval = 800 , s , e ,timer , el;
		this.addEventListener(tStart, function(e) {
			var _e = util.getEventInfo(e);
			el = _e.target;
			s = Date.now();
			timer = setTimeout(function() {
				_e.type = 'longtap';
				callback.call(el,_e);
			}, interval);
		}, false);

		this.addEventListener(tEnd, function(e) {
			clearTimeout(timer);
		}, false);
	};

	/****************
	@deprecated 滑动
	*****************/
	var _swipe = function(callback) {
		var s = {}, e = {}, d = {}, distance = 50,
			angle = 0,
			type;

		this.addEventListener(tStart, function(evt) {
			var _e = util.getEventInfo(evt);
			s.x = _e.pageX;
			s.y = _e.pageY;
			evt.preventDefault();
		}, false);

		this.addEventListener(tEnd, function(evt) {
			var _e = util.getEventInfo(evt);
			e.x = _e.pageX;
			e.y = _e.pageY;

			d.x = e.x - s.x;
			d.y = e.y - s.y;

			if (Math.abs(d.x) < distance && Math.abs(d.y) < distance) return false;

			angle = Math.abs(Math.atan((e.y - s.y) / (e.x - s.x)) / Math.PI * 180);

			if (angle > 45) {
				type = d.y < 0 ? 'swipe-up' : 'swipe-down';
			} else {
				type = d.x < 0 ? 'swipe-left' : 'swipe-right';
			}

			callback.call(this, {
				type: type,
				start: s,
				end: e,
				distance: d,
				target:_e.target
			});
			evt.preventDefault();
		}, false);
	}

	/**********
	@deprecated 拖拽
	**********/
	var _drag = function(callback) {
		var dragStart = false, s={},e={},o={};
		this.addEventListener(tStart, function(evt) {
			dragStart = true;
			var _e = util.getEventInfo(evt);
			s.x = _e.pageX;
			s.y = _e.pageY;
			evt.preventDefault();
		}, false);

		this.addEventListener(tMove, function(evt) {
			if (!dragStart) return;
			var _e = util.getEventInfo(evt);

			o.x = _e.pageX - s.x;
			o.y = _e.pageY - s.y;

			callback.call(this,{
				type : "drag-move",
				start : s,
				offset : o,
				target : _e.target
			});

		}, false);

		this.addEventListener(tEnd, function(evt) {
			dragStart = false;
			var _e = util.getEventInfo(evt);
			e.x = _e.pageX;
			e.y = _e.pageY;

			callback.call(this,{
				type : "drag-end",
				start : s,
				offset : o,
				end : e,
				target : _e.target
			});
		}, false);
	}

	var eventMap = {
		tap: _tap,
		swipe: _swipe,
		longtap:_longtap,
		drag : _drag
	}

	var finger = {

		ver: _ver,

		on: function(el, type, fn) {
			try {
				var el = util.$(el);
				eventMap[type].call(el, fn);
			} catch (e) {
				console.error('type error : %s is not allowed', type);
			}

		},

		off: function(el, type) {

		}
	}

	window.fingerJS = finger;

})();
